package com.qn.visitor.analyze.resolver;


import com.qn.visitor.model.UserVisitorRequest;
import com.qn.visitor.model.bo.analyze.UserAgentModel;
import com.qn.visitor.resources.ResourcesCore;
import com.qn.visitor.resources.ResourcesUserAgent;
import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@Component
public class UserAgentResolver implements IResolver<UserAgentModel> {
    @Autowired
    public UserAgentAnalyzer userAgentAnalyzer;

    @Override
    public UserAgentModel rsolver(UserVisitorRequest ubrBo) throws Exception {
        UserAgent agent = userAgentAnalyzer.parse(ubrBo.getUserAgent());
        UserAgentModel uam = new UserAgentModel();

        uam.setDeviceClass(emptying(agent.getValue(ResourcesUserAgent.DEVICE_CLASS)));
        uam.setDeviceBrand(emptying(agent.getValue(ResourcesUserAgent.DEVICE_BRAND)));
        uam.setDeviceVersion(emptying(agent.getValue(ResourcesUserAgent.DEVICE_VERSION)));
        //如果设备具体版本获取不到 先取固件版本
        if (StringUtils.hasText(uam.getDeviceVersion())) {
            uam.setDeviceVersion(emptying(agent.getValue(ResourcesUserAgent.DEVICE_FIRMWARE_VERSION)));
        }
        //在获取设备名称
        if (StringUtils.hasText(uam.getDeviceVersion())) {
            uam.setDeviceVersion(emptying(agent.getValue(ResourcesUserAgent.DEVICE_NAME)));
        }
        if (ResourcesUserAgent.DESKTOP.equals(uam.getDeviceVersion())) {
            uam.setDeviceVersion(null);
        }
        //设备版本中剔除设备 厂家重复名称
        if (!StringUtils.hasText(uam.getDeviceVersion()) &&
                !StringUtils.hasText(uam.getDeviceBrand())) {
            uam.setDeviceVersion(
                    emptying(uam.getDeviceVersion()
                            .replaceAll(uam.getDeviceBrand(), ""))
            );
        }

        uam.setSystemClass(emptying(agent.getValue(ResourcesUserAgent.OPERATING_SYSTEM_CLASS)));
        uam.setSystemName(emptying(agent.getValue(ResourcesUserAgent.OPERATING_SYSTEM_NAME)));
        uam.setSystemVersion(emptying(agent.getValue(ResourcesUserAgent.OPERATING_SYSTEM_VERSION)));

        String webviewAppName = emptying(agent.getValue(ResourcesUserAgent.WEBVIEW_APP_NAME));
        uam.setBrowserType(emptying(agent.getValue(ResourcesUserAgent.AGENT_CLASS)));
        if (!StringUtils.hasText(webviewAppName) && !ResourcesCore.UNKNOWN.equals(webviewAppName)) {
            uam.setBrowserName(emptying(webviewAppName));
            uam.setBrowserVersion(emptying(agent.getValue(ResourcesUserAgent.WEBVIEW_APP_VERSION)));
            if (webviewAppName.indexOf(ResourcesUserAgent.APP) != -1) {
                uam.setBrowserType(ResourcesUserAgent.MOBILE_APP);
            }
        } else {
            uam.setBrowserName(emptying(agent.getValue(ResourcesUserAgent.AGENT_NAME)));
            uam.setBrowserVersion(emptying(agent.getValue(ResourcesUserAgent.AGENT_VERSION)));
        }
        return uam;
    }

    public String emptying(String str) {
        if (!StringUtils.hasText(str)) {
            if (ResourcesCore.UNKNOWN.equals(str) || ResourcesCore.QUESTION_MARK.equals(str)) {
                return null;
            }
        }
        return str.trim();
    }
}
